package com.thinkgem.jeesite.modules.erp.uitl;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * c3p0连接池Util
 *
 */
public class DBUtil {
	private static ComboPooledDataSource ds = null;
	
	//设置属性
	static{
        try{
            ds = new ComboPooledDataSource();
            InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties prop = new Properties();
            prop.load(in);
            String driverClass = prop.getProperty("driverClass");
            String jdbcUrl = prop.getProperty("jdbcUrl");
            String user = prop.getProperty("user");
            String password = prop.getProperty("password");
            String acquireIncrement = prop.getProperty("acquireIncrement");
            String initialPoolSize = prop.getProperty("initialPoolSize");
            String minPoolSize = prop.getProperty("minPoolSize");
            String maxPoolSize = prop.getProperty("maxPoolSize");
            String checkoutTimeout = prop.getProperty("checkoutTimeout");
            String acquireRetryAttempts = prop.getProperty("acquireRetryAttempts");
            String acquireRetryDelay = prop.getProperty("acquireRetryDelay");
            String testConnectionOnCheckin = prop.getProperty("testConnectionOnCheckin");

            ds.setDriverClass(driverClass);
            ds.setJdbcUrl(jdbcUrl);
            ds.setUser(user);
            ds.setPassword(password);
            ds.setAcquireIncrement(Integer.parseInt(acquireIncrement));
            ds.setInitialPoolSize(Integer.parseInt(initialPoolSize));
            ds.setMinPoolSize(Integer.parseInt(minPoolSize));
            ds.setMaxPoolSize(Integer.parseInt(maxPoolSize));
            ds.setCheckoutTimeout(Integer.parseInt(checkoutTimeout));
            ds.setAcquireRetryAttempts(Integer.parseInt(acquireRetryAttempts));
            ds.setAcquireRetryDelay(Integer.parseInt(acquireRetryDelay));
            ds.setTestConnectionOnCheckin(Boolean.valueOf(testConnectionOnCheckin));

        }catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }
	//获取连接对象
	public static Connection getConnection() throws SQLException{
        return ds.getConnection();
    }
	
	//释放
	public static void release(Connection conn,PreparedStatement pstmt,ResultSet rs) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(pstmt!=null){
            try {
                pstmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            pstmt = null;
        }

        if(conn!=null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
}
